﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.PetroleumDataPartLib.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for FeatureLayEditWindow.xaml
    /// </summary>
    public partial class FeatureLayEditWindow : Window
    {
        public List<FeatureLayer> RetFeatureLayerList { get; private set; }
        private ObservableCollection<FeatureLayer> _FeatureLayerList;
        public FeatureLayEditWindow(List<FeatureLayer> featrueLayerList)
        {
            InitializeComponent();
            _FeatureLayerList = new ObservableCollection<FeatureLayer>();
            if (featrueLayerList != null)
            {
                foreach (var item in featrueLayerList)
                {
                    _FeatureLayerList.Add(item);
                }
            }
            RetFeatureLayerList = new List<FeatureLayer>();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _ctlLV.ItemsSource = _FeatureLayerList;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            FeatureLayer featureLayer = new FeatureLayer();
            _FeatureLayerList.Add(featureLayer);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlLV.SelectedItem != null && _ctlLV.SelectedIndex >= 0)
            {
                FeatureLayer featureLayer = _ctlLV.SelectedItem as FeatureLayer;
                _FeatureLayerList.Remove(featureLayer);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            RetFeatureLayerList.Clear();
            foreach (var item in _FeatureLayerList)
            {
                if (string.IsNullOrWhiteSpace(item.Url) || !IsUri(item.Url) || string.IsNullOrWhiteSpace(item.LayerId))
                {
                    CustomMessageBox.ShowWarning("请输入合法的Url和LayerID");
                    return;
                }
                RetFeatureLayerList.Add(item);
            }
            DialogResult = true;
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }
        public bool IsUri(string s)
        {
            Uri u;
            return Uri.TryCreate(s, UriKind.Absolute, out u);
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            var aa = (sender as FrameworkElement).DataContext;
            _ctlLV.SelectedItem = aa;
        }
    }
}
